/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.account;

import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.tools.IvanBiz;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Ivan
 */
public class AccountAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();

    public ActionForward accountList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
        AccountForm accountForm = (AccountForm) form;
        HttpSession session = request.getSession(true);
        try {
            List listAccount = accountService.loadAll();
            request.setAttribute(SISConstants.LIST_ACCOUNT, listAccount);
            session.removeAttribute(SISConstants.LIST_ACCOUNT_CARD);
            session.removeAttribute(SISConstants.LIST_ACCOUNT_RELATION);
//            session.removeAttribute("listUser");
            session.removeAttribute(SISConstants.ACCOUNT_FORM);
        } catch (Exception ex) {
            Logger.getLogger(AccountAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_LIST);
    }

    public ActionForward accountAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
//            UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
//        try {
//            List listUser = userService.loadAll();
//            request.setAttribute("listUser", listUser);
//        } catch (Exception ex) {
//            Logger.getLogger(AccountAction.class.getName()).log(Level.SEVERE, null, ex);
//            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
//            saveMessages(request, actionMessages);
//            return new ActionForward(mapping.getInput());
//        }        
        return mapping.findForward(SISScreenConstants.ACCOUNT_ADD);
    }

    public ActionForward accountSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
//        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        AccountForm accountForm = (AccountForm) form;
        Account account = new Account();
        HttpSession session = request.getSession(true);
        try {
            boolean accountIsUsed = accountService.cekIsEmptyKode(accountForm.getKode());
            if (accountIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CODE_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.ACCOUNT_ADD);
            } else {
                account.setKode(accountForm.getKode());
                account.setNama(accountForm.getNama());
                account.setAlamat(accountForm.getAlamat());
                account.setTelp(accountForm.getTelp());
                account.setStatusJabatan(accountForm.getStatusJabatan());
//                User user = userService.loadById(accountForm.getUser());
//                account.setUser(user);
                account.setAccountCard((List) session.getAttribute(SISConstants.LIST_ACCOUNT_CARD));
                account.setAccountRelations((List) session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION));
                accountService.saveAccount(account);
                session.removeAttribute(SISConstants.LIST_ACCOUNT_CARD);
                session.removeAttribute(SISConstants.LIST_ACCOUNT_RELATION);
//                session.removeAttribute("listUser");
                session.removeAttribute(SISConstants.ACCOUNT_FORM);
                accountForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(AccountAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT);
    }

    public ActionForward accountEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
//        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        AccountForm accountForm = (AccountForm) form;
        HttpSession session = request.getSession(true);
        try {
            session.setAttribute(SISConstants.ACCOUNT_ID, request.getParameter(SISConstants.ID).toString());
            Account account = (Account) accountService.loadById(Long.parseLong((String) session.getAttribute(SISConstants.ACCOUNT_ID)));
            accountForm.setKode(account.getKode());
            accountForm.setNama(account.getNama());
            accountForm.setAlamat(account.getAlamat());
            accountForm.setTelp(account.getTelp());
            accountForm.setStatusJabatan(account.getStatusJabatan());
            //accountForm.setUser(account.getUser().getId());
//            List listUser = userService.loadAll();
//            session.setAttribute("listUser", listUser);
            session.setAttribute(SISConstants.ACCOUNT_FORM, accountForm);
            if (session.getAttribute(SISConstants.LIST_ACCOUNT_CARD) == null) {
                session.setAttribute(SISConstants.LIST_ACCOUNT_CARD, account.getAccountCard());
            }
            if (session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION) == null) {
                session.setAttribute(SISConstants.LIST_ACCOUNT_RELATION, account.getAccountRelations());
            }
        } catch (Exception ex) {
            Logger.getLogger(AccountAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_EDIT);
    }

    public ActionForward accountUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
//        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        AccountForm accountForm = (AccountForm) form;
        HttpSession session = request.getSession(true);
        try {
            boolean accountIsUsed = accountService.cekIsEmptyKode(accountForm.getKode());
            if (accountIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CODE_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.ACCOUNT_EDIT);
            } else {
                List list = (List) session.getAttribute(SISConstants.LIST_ACCOUNT_CARD);
                for (int x = 0; x < list.size(); x++) {
                    AccountCard accountCard = (AccountCard) list.get(x);
                    if (accountCard.getNama().equals(SISConstants.PERPUSTAKAAN)) {
                        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.EMPTY_STRING));
                        saveMessages(request, actionMessages);
                        return new ActionForward(mapping.getInput());
                    }
                }
                Account account = accountService.loadById(accountForm.getId());
                account.setKode(accountForm.getKode());
                account.setNama(accountForm.getNama());
                account.setAlamat(accountForm.getAlamat());
                account.setTelp(accountForm.getTelp());
                account.setStatusJabatan(accountForm.getStatusJabatan());
//                User user = userService.loadById(accountForm.getUser());
                //account.setUser(user);
                account.setAccountCard((List) session.getAttribute(SISConstants.LIST_ACCOUNT_CARD));
                account.setAccountRelations((List) session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION));
                accountService.updateAccount(account);
                session.removeAttribute(SISConstants.LIST_ACCOUNT_CARD);
                session.removeAttribute(SISConstants.LIST_ACCOUNT_RELATION);
//                session.removeAttribute("listUser");
                accountForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(AccountAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT);
    }

    public ActionForward accountDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
        try {
            Account account = accountService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            accountService.deleteAccount(account);
        } catch (Exception ex) {
            Logger.getLogger(AccountAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT);
    }

    public ActionForward accountDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
        try {
            Account detailAccount = (Account) accountService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            request.setAttribute(SISConstants.DETAIL_ACCOUNT, detailAccount);
            request.setAttribute(SISConstants.LIST_ACCOUNT_CARD, detailAccount.getAccountCard());
            request.setAttribute(SISConstants.LIST_ACCOUNT_RELATION, detailAccount.getAccountRelations());
        } catch (Exception ex) {
            Logger.getLogger(AccountAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_DETAIL);
    }

    public ActionForward accountCardAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_CARD) == null) {
            List listAccountCard = new ArrayList();
            session.setAttribute(SISConstants.LIST_ACCOUNT_CARD, listAccountCard);
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_CARD_ADD);
    }

    public ActionForward accountCardSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_CARD) != null) {
            List listAccountCard = (List) session.getAttribute(SISConstants.LIST_ACCOUNT_CARD);
            AccountForm accountForm = (AccountForm) form;
            boolean cek = true;
            for (int x = 0; x < listAccountCard.size(); x++) {
                AccountCard accountCardTemp = (AccountCard) listAccountCard.get(x);
                if (accountForm.getNoCard().equals(accountCardTemp.getNoCard()) && accountForm.getNamaCard().equals(accountCardTemp.getNama())) {
                    cek = false;
                    actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CARD_IS_EXIST));
                    saveMessages(request, actionMessages);
                    return mapping.findForward(SISScreenConstants.ACCOUNT_CARD_ADD);
                }
            }
            if (cek == true) {
                AccountCard accountCard = new AccountCard();
                accountCard.setNoCard(accountForm.getNoCard());
                accountCard.setNama(accountForm.getNamaCard());
                listAccountCard.add(accountCard);
                session.setAttribute(SISConstants.LIST_ACCOUNT_CARD, listAccountCard);
                accountForm.reset(mapping, request);
            }
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_CARD_ADD);
    }

    public ActionForward accountCardEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_CARD) == null) {
            List listAccountCard = new ArrayList();
            session.setAttribute(SISConstants.LIST_ACCOUNT_CARD, listAccountCard);
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_CARD_EDIT);
    }

    public ActionForward accountCardUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_CARD) != null) {
            List listAccountCard = (List) session.getAttribute(SISConstants.LIST_ACCOUNT_CARD);
            AccountForm accountForm = (AccountForm) form;
            boolean cek = true;
            for (int x = 0; x < listAccountCard.size(); x++) {
                AccountCard accountCardTemp = (AccountCard) listAccountCard.get(x);
                if (accountForm.getNoCard().equals(accountCardTemp.getNoCard()) && accountForm.getNamaCard().equals(accountCardTemp.getNama())) {
                    cek = false;
                    actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CARD_IS_EXIST));
                    saveMessages(request, actionMessages);
                    return mapping.findForward(SISScreenConstants.ACCOUNT_CARD_EDIT);
                }
            }
            if (cek == true) {
                AccountCard accountCard = new AccountCard();
                accountCard.setNoCard(accountForm.getNoCard());
                accountCard.setNama(accountForm.getNamaCard());
                listAccountCard.add(accountCard);
                session.setAttribute(SISConstants.LIST_ACCOUNT_CARD, listAccountCard);
                accountForm.reset(mapping, request);
            }
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_CARD_EDIT);
    }

    public ActionForward accountCardAddDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_CARD) != null) {
            List listAccountCard = (List) session.getAttribute(SISConstants.LIST_ACCOUNT_CARD);
            for (int x = 0; x < listAccountCard.size(); x++) {
                AccountCard accountCard = (AccountCard) listAccountCard.get(x);
                if (accountCard.getNoCard().equals(request.getParameter(SISConstants.NO_CARD))) {
                    listAccountCard.remove(x);
                    break;
                }
            }
            session.setAttribute(SISConstants.LIST_ACCOUNT_CARD, listAccountCard);
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_CARD_ADD);
    }

    public ActionForward accountCardEditDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_CARD) != null) {
            List listAccountCard = (List) session.getAttribute(SISConstants.LIST_ACCOUNT_CARD);
            for (int x = 0; x < listAccountCard.size(); x++) {
                AccountCard accountCard = (AccountCard) listAccountCard.get(x);
                if (accountCard.getNoCard().equals(request.getParameter(SISConstants.NO_CARD))) {
                    listAccountCard.remove(x);
                    break;
                }
            }
            session.setAttribute(SISConstants.LIST_ACCOUNT_CARD, listAccountCard);
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_CARD_EDIT);
    }

    public ActionForward accountRelationAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION) == null) {
            List listAccountRelation = new ArrayList();
            session.setAttribute(SISConstants.LIST_ACCOUNT_RELATION, listAccountRelation);
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_RELATION_ADD);
    }

    public ActionForward accountRelationSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION) != null) {
            List listAccountRelation = (List) session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION);
            AccountForm accountForm = (AccountForm) form;
            AccountRelations accountRelations = new AccountRelations();
            accountRelations.setNama(accountForm.getNamaRelation());
            accountRelations.setUmur(accountForm.getUmur());
            accountRelations.setRelasi(accountForm.getRelasi());
            listAccountRelation.add(accountRelations);
            session.setAttribute(SISConstants.LIST_ACCOUNT_RELATION, listAccountRelation);
            accountForm.reset(mapping, request);
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_RELATION_ADD);
    }

    public ActionForward accountRelationEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION) == null) {
            List listAccountRelation = new ArrayList();
            session.setAttribute(SISConstants.LIST_ACCOUNT_RELATION, listAccountRelation);
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_RELATION_EDIT);
    }

    public ActionForward accountRelationUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION) != null) {
            List listAccountRelation = (List) session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION);
            AccountForm accountForm = (AccountForm) form;
            AccountRelations accountRelations = new AccountRelations();
            accountRelations.setNama(accountForm.getNamaRelation());
            accountRelations.setUmur(accountForm.getUmur());
            accountRelations.setRelasi(accountForm.getRelasi());
            listAccountRelation.add(accountRelations);
            session.setAttribute(SISConstants.LIST_ACCOUNT_RELATION, listAccountRelation);
            accountForm.reset(mapping, request);
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_RELATION_EDIT);
    }

    public ActionForward accountRelationAddDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION) != null) {
            List listAccountRelation = (List) session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION);
            for (int x = 0; x < listAccountRelation.size(); x++) {
                AccountRelations accountRelations = (AccountRelations) listAccountRelation.get(x);
                if (accountRelations.getNama().equals(request.getParameter(SISConstants.NAMA))) {
                    listAccountRelation.remove(x);
                    break;
                }
            }
            session.setAttribute(SISConstants.LIST_ACCOUNT_RELATION, listAccountRelation);
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_RELATION_ADD);
    }

    public ActionForward accountRelationEditDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION) != null) {
            List listAccountRelation = (List) session.getAttribute(SISConstants.LIST_ACCOUNT_RELATION);
            for (int x = 0; x < listAccountRelation.size(); x++) {
                AccountRelations accountRelations = (AccountRelations) listAccountRelation.get(x);
                if (accountRelations.getNama().equals(request.getParameter(SISConstants.NAMA))) {
                    listAccountRelation.remove(x);
                    break;
                }
            }
            session.setAttribute(SISConstants.LIST_ACCOUNT_RELATION, listAccountRelation);
        }
        return mapping.findForward(SISScreenConstants.ACCOUNT_RELATION_EDIT);
    }
}
